/**
 Copyright (c) 2020-2022 宁波哥爱帮科技有限公司
 */
package cn.grab.fispservice.authentication.service.impl;

import cn.grab.fispapi.authentication.vo.EnterpriseAuthenticationVO;
import cn.grab.fispservice.authentication.mapper.EnterpriseAuthenticationMapper;
import cn.grab.fispservice.authentication.mapper.UserAuthenticationMapper;
import cn.grab.fispservice.authentication.model.EnterpriseAuthentication;
import cn.grab.fispservice.authentication.model.UserAuthentication;
import cn.grab.fispservice.authentication.service.EnterpriseAuthenticationService;
import cn.grab.fispservice.util.ThreadLocalUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fontana.base.result.Result;
import com.fontana.base.result.ResultCode;
import com.fontana.db.mapper.BaseDaoMapper;
import com.fontana.db.service.impl.AbstractBaseService;
import com.fontana.util.bean.BeanUtil;
import javax.annotation.RegEx;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @description 企业认证服务类实现类
 * @author 16411
 * @date 2022/12/14 15:20
 */
@Service
public class EnterpriseAuthenticationServiceImpl extends AbstractBaseService<EnterpriseAuthentication,Long> implements EnterpriseAuthenticationService {

  @Resource
  private EnterpriseAuthenticationMapper enterpriseAuthenticationMapper;

  @Resource
  private UserAuthenticationMapper userAuthenticationMapper;


  @Override
  protected BaseDaoMapper<EnterpriseAuthentication> mapper() {
    return enterpriseAuthenticationMapper;
  }

  /**
   *@Param:
   *@return: PersonalAuthenticationVO 企业认证视图实体类
   *@Author: xxxx
   *@date: 2022/12/23
   */
  @Override
  public Result<Object> findEnterpriseAuthentication() {
    //获取当前登录的用户id
    Long userId = ThreadLocalUtils.get();
    if (null == userId ){
      return Result.failed(ResultCode.USER_NOT_LOGGED_IN,"当前未登录,请登录后重试!");
    }

    //根据当前用户id查询用户认证关联表信息
    UserAuthentication userAuthentication = userAuthenticationMapper.selectById(userId);

    //根据中间表信息获取企业认证信息
    EnterpriseAuthentication enterpriseAuthentication = enterpriseAuthenticationMapper.selectById(
        userAuthentication.getEnterpriseAuthenticationId());

    //当企业信息为null,代表还未进行认证
    if (enterpriseAuthentication == null){
      return Result.failed(ResultCode.FAIL,"当前用户未认证");
    }

    //获取当前认证信息是否已经审核
    if (enterpriseAuthentication.getWhetherExamine() == 0){
      return Result.failed("请等待管理员审核认证");
    } else if (enterpriseAuthentication.getWhetherExamine() == 1){
      return Result.failed("认证失败!请填写正确信息重新认证");
    }

    //拷贝并创建企业认证实体类
    EnterpriseAuthenticationVO enterpriseAuthenticationVO = BeanUtil.copy(enterpriseAuthentication,
        EnterpriseAuthenticationVO.class);

    //返回企业认证信息
    return Result.failed(enterpriseAuthenticationVO,ResultCode.SUCCESS);
  }
}
